<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
#if($tt)
<%@page import="com.joinway.framework.bean.domain.constant.SqlConstants.*" %>
#end
<style type="text/css">
#${table}_${rnd}_dlg{
	padding:10px 20px;
}
#${table}_${rnd}_form table{
	width:100%;
	border-collapse:collapse;
	border-right:1px solid #A9A9A9;
}
#${table}_${rnd}_form th{
	border-left:1px solid #A9A9A9;
	border-top:1px solid #A9A9A9;
	border-bottom:1px solid #A9A9A9;
	border-right:1px solid #D3D3D3;
	padding-left:10px;
	padding-right:10px;
	text-align:right;
}
#${table}_${rnd}_form td{
	border-top:1px solid #A9A9A9;
	border-bottom:1px solid #A9A9A9;
	padding-top:5px;
	padding-bottom:5px;
	padding-left:5px;
	text-align:left;
}
</style>

<script type="text/javascript">
var ${table}Table${rnd} = '${table}';
var ${table}Query${rnd};
var wysiwye${rnd} = false;	// what you see is what you export

$(function() {
#if(${orders})
	var query = {
		"order": [
#foreach($order in $orders)		          
			{
				"field":"${order.name}",
				"sort":"${order.order}"
			}#if($velocityCount != $orders.size()),#end
#end			
		]
	};

	initDataGrid('#${table}_${rnd}_grid', appName + 'search/' + ${table}Table${rnd} + '.json?init=' + JSON.stringify(query));
#else
	initDataGrid('#${table}_${rnd}_grid', 'search/' + ${table}Table${rnd} + '.json');
#end
	${table}Query${rnd} = '';

	initUploadFileForm($('#${table}_${rnd}_file_form'), $('#${table}_${rnd}_columnMap'), $('#${table}_${rnd}_grid'), wysiwye${rnd});
});


/**
 * 查询
 */
function search${Table}${rnd}() {
	${table}Query${rnd} = get${Table}QueryJson${rnd}();
	searchDataGrid('#${table}_${rnd}_grid', ${table}Query${rnd});
}

/**
 * 查询条件
 */
function get${Table}QueryJson${rnd}(){
	var query = {
		"items":[
#foreach($field in $qfields)
		    {
				"field": "${field.name}",
#if(${field.type} == 'Date')
				"operator": "between",
				"type": "date",
				"value": [
					$('#${table}_${rnd}_${field.name}_from').datebox('getValue'),
					$('#${table}_${rnd}_${field.name}_to').datebox('getValue')
				]
#elseif(${field.type} == 'Number' || ${field.type} == 'Decimal')
				"operator": "between",
				"type": "number",
				"value": [
					$('#${table}_${rnd}_${field.name}_from').numberbox('getValue'),
					$('#${table}_${rnd}_${field.name}_to').numberbox('getValue')
				]
#else
				"operator": "like",
				"value": $('#${table}_${rnd}_${field.name}').val()
#end
			}#if($velocityCount != $qfields.size()),#end
#end
		]#if(${orders}),
		"order": [
#foreach($order in $orders)
			{
				"field":"${order.name}",
				"sort":"${order.order}"
			}#if($velocityCount != $orders.size()),#end
#end
		]
#end
	}

	return query;
}

/**
 * 刷新
 */
//function refresh${Table}${rnd}() {
//	refreshDataGrid('#${table}_grid');
//}

/** 
 * 添加
 */
function add${Table}Item${rnd}(){
	showAddDialog('#${table}_${rnd}_form', '#${table}_${rnd}_dlg');
 
	// 设置每一个radio最后一项选中
	$('#${table}_${rnd}_form').children().find(':radio').each(function(){
		$(this).prop('checked', true);
	});
	
	$('#${table}_${rnd}_save').unbind('click');
	$('#${table}_${rnd}_save').click(function(){
	 	var valid = true;
	 	/* 如果要进行数据重复校验，在这里添检查ajax调用
	 	 * 以下例子用来检查单个字段是否重复
	 	 */
	 	//valid = checkExistence(${table}Table${rnd}, '数据已存在', '<fieldName>', $('#${table}_<fieldName>_input').val());
	 	
	 	if(valid){
#if($ctt)
 			reset${Table}CTimestampItem${rnd}();
#end
#if($ced)
 			reset${Table}CEditorItem${rnd}();
#end
			
			$('#${table}_${rnd}_${autoIdField}_input').numberbox('setValue', '0');
			saveItem({jgridId:'#${table}_${rnd}_grid', jformId:'#${table}_${rnd}_form', jdlgId:'#${table}_${rnd}_dlg', table:${table}Table${rnd}, callback:null});
	 	}
 	});
}

/**
 * 编辑
 */
function edit${Table}Item${rnd}(){
	var flag = showEditDialog('#${table}_${rnd}_grid', '#${table}_${rnd}_form', '#${table}_${rnd}_dlg');
 
	if(flag){
	 	var valid = true;
	 	/* 如果要进行数据重复校验，在这里添检查ajax调用
	 	 * 以下例子用来检查单个字段是否重复
	 	 */
	 	//valid = checkExistence(${table}Table${rnd}, '数据已存在', '<fieldName>', $('#${table}_<fieldName>_input').val());
	 	
	 	if(valid){
#if($tt)
			reset${Table}TimestampItem${rnd}();
#end
#if($ed)
			reset${Table}EditorItem${rnd}();
#end
			$('#${table}_${rnd}_save').unbind('click');
			$('#${table}_${rnd}_save').click(function(){
				saveItem({jgridId:'#${table}_${rnd}_grid', jformId:'#${table}_${rnd}_form', jdlgId:'#${table}_${rnd}_dlg', table:${table}Table${rnd}, callback:null});
			});
	 	}
	}
}

/**
 * 复制
 */
function copy${Table}Item${rnd}(){
	var flag = showCopyDialog('#${table}_${rnd}_grid', '#${table}_${rnd}_form', '#${table}_${rnd}_dlg');
	
	if(flag){
 		/* 在这里重设需要初始化的字段 */
 		// 默认将自增主键赋值为0
		_.each([${idFields}], function(element){
			$('#${table}_${rnd}_form').children().find('input[name="' + element + '"]').each(function(){
				$(this).val('0');
			});
		});
		
#if($ctt)
		reset${Table}CTimestampItem${rnd}();
#end
#if($ced)
		reset${Table}CEditorItem${rnd}();
#end
		
#if($ed)
		// 清空更新者
		_.each([$ed], function(element){
			$('#${table}_${rnd}_form').children().find('input[name="' + element + '"]').each(function(){
				$(this).val('');
			});
		});		
#end

#if($tt)
		// 清空更新时间
		_.each([$tt], function(element){
			$('#${table}_${rnd}_form').children().find('input[name="' + element + '"]').each(function(){
				$(this).datetimebox({value:''});
			});
		});		
#end
		
		$('#${table}_${rnd}_save').unbind('click');
		$('#${table}_${rnd}_save').click(function(){
		 	var valid = true;
		 	/* 如果要进行数据重复校验，在这里添检查ajax调用
		 	 * 以下例子用来检查单个字段是否重复
		 	 */
		 	//valid = checkExistence(${table}Table${rnd}, '数据已存在', '<fieldName>', $('#${table}_<fieldName>_input').val());
		 	
		 	if(valid){
		 		saveItem({jgridId:'#${table}_${rnd}_grid', jformId:'#${table}_${rnd}_form', jdlgId:'#${table}_${rnd}_dlg', table:${table}Table${rnd}, callback:null});
		 	}
		});
	}
}

#if($tt)
/**
 * 设置更新时间戳
 */
function reset${Table}TimestampItem${rnd}(){
	var fields = [$tt];
	$('#${table}_${rnd}_form').children().find('input').each(function(){
		if(_.contains(fields, $(this).attr('name'))){
			$(this).val('<%=ReservedValue.CURRENT_TIMESTAMP%>');
		}
	});
}
#end

#if($ed)
/**
 * 设置更新者
 */
function reset${Table}EditorItem${rnd}(){
	var fields = [$ed];
	$('#${table}_${rnd}_form').children().find('input').each(function(){
		if(_.contains(fields, $(this).attr('name'))){
			$(this).val('${userContext.user.loginName}');
		}
	});
}
#end

#if($ctt)
/**
 * 设置创建时间戳
 */
function reset${Table}CTimestampItem${rnd}(){
	var fields = [$ctt];
	$('#${table}_${rnd}_form').children().find('input').each(function(){
		if(_.contains(fields, $(this).attr('name'))){
			$(this).val('<%=ReservedValue.CURRENT_TIMESTAMP%>');
		}
	});
}
#end

#if($ced)
/**
 * 设置创建人
 */
function reset${Table}CEditorItem${rnd}(){
	var fields = [$ced];
	$('#${table}_${rnd}_form').children().find('input').each(function(){
		if(_.contains(fields, $(this).attr('name'))){
			$(this).val('${userContext.user.loginName}');
		}
	});
}
#end
	
/**
 * 导出查询结果
 */
function export${Table}Query${rnd}(type){
	var suffix = type == 'Excel' ? 'xls' : 'csv';
	
	showConfirmMessage('是否以' + type + '格式导出查询结果?', function(r){
		if(r){
			var form = new Object();

			form.columns = getExportColumns($('#${table}_${rnd}_grid'), wysiwye${rnd});
			form.table = ${table}Table${rnd};
			
			var query = ${table}Query${rnd} ? ${table}Query${rnd} : get${Table}QueryJson${rnd}();
			form.query = JSON.stringify(query);
			
			// 表格显示的名称
			form.tableDisplayName = $(jtabId).tabs('getSelected').panel('options').title;
			// 下载文件的名称
			form.fileName = $(jtabId).tabs('getSelected').panel('options').title;
			// 下载文件格式
			form.type = suffix;
			
			form.port = window.location.port;
			
			downloadFile(form);
		}
	});
}

#foreach($field in $fields)
#if($field.radios)
/**
 * 格式化${field.name}字段
 */
function ${Table}${field.uname}Formatter${rnd}(value, row, index){
	var text;
	var v = value + '';
	switch(v){
#foreach($item in $field.radios)
		case '${item.value}':
			text = '${item.text}';
			break;
#end
		default:
			text = value;
	}
	return text;
}
#end
#end

function resize${Table}${rnd}(){
	$('#${table}_${rnd}_grid').datagrid('resize');
}
</script>

<table id="${table}_${rnd}_grid" data-options="toolbar:'#${table}_${rnd}_toolbar'">
    <thead>
        <tr>
        	<th data-options="field:'ck'" checkbox="true"></th>
#foreach($field in $fields)
#if($field.radios)
			<th data-options="field:'${field.name}',hidden:${field.visible},formatter:${Table}${field.uname}Formatter${rnd}">${field.text}</th>
#else
			<th data-options="field:'${field.name}',hidden:${field.visible}">${field.text}</th>
#end
#end 
		</tr>
    </thead>
</table>

<div id="${table}_${rnd}_toolbar">
	<div style="display:none">
		<form id="${table}_${rnd}_file_form" enctype="multipart/form-data" method="post" action="file/import.json">
			<input id="${table}_${rnd}_file" type="file" name="file" accept="" onchange="startUpload($('#${table}_${rnd}_file_form'));"/>
			<input id="${table}_${rnd}_targetTable" name="table" type="text" value="${table}">
			<input id="${table}_${rnd}_columnMap" name="columnMap" type="text">
			<input id="${table}_${rnd}_file_submit" type="submit"/>
		</form>
		<iframe id="${table}_${rnd}_file_target" name="${table}_${rnd}_file_target"></iframe>
	</div>

	<div id="${table}_${rnd}_querybar" title="查询条件" class="easyui-panel" data-options="collapsible:true,border:false,onCollapse:resize${Table}${rnd},onExpand:resize${Table}${rnd}">
		<table>
#foreach($field in $conditions)
#if($velocityCount % $maxColumn == 1)
			<tr>
#end##$velocityCount % $maxColumn == 1
				<td style="text-align:right;">${field.text}</td>
				<td style="text-align:left;padding-left:0px;padding-right:10px;">
#if(${field.category} == 'Date')
					从<input id="${table}_${rnd}_${field.name}_from" class="easyui-datebox" style="width:100px">&nbsp;到&nbsp;<input id="${table}_${rnd}_${field.name}_to" class="easyui-datebox" style="width:100px">
#elseif(${field.category} == 'Date' || ${field.category} == 'DateTime' || ${field.category} == 'Timestamp')
					从<input id="${table}_${rnd}_${field.name}_from" class="easyui-datetimebox" style="width:100px">&nbsp;到&nbsp;<input id="${table}_${rnd}_${field.name}_to" class="easyui-datetimebox" style="width:100px">
#elseif(${field.category} == 'Number' || ${field.category} == 'Decimal')
					从<input id="${table}_${rnd}_${field.name}_from" class="easyui-numberbox" style="width:100px">&nbsp;到&nbsp;<input id="${table}_${rnd}_${field.name}_to" class="easyui-numberbox" style="width:100px">
#elseif($field.radios)
					<select id="${table}_${rnd}_${field.name}" style="width:100%">
						<option value="" selected>全部</option>
#foreach($item in $field.radios)
						<option value="${item.value}">${item.text}</option>
#end
					</select>
#else
					<input id="${table}_${rnd}_${field.name}" style="width:100%" type="text"/>
#end##${field.category} == 'Date'
				</td>
#if($velocityCount == $maxColumn || $velocityCount < $maxColumn && $velocityCount == $conditions.size())
				<td style="padding-left:10px;">
					<a href="#" class="easyui-linkbutton" iconCls="icon-search" onclick="search${Table}${rnd}()">查询</a>
					<a href="#" class="easyui-linkbutton" iconCls="icon-clear" onclick="clearCriteria('#${table}_${rnd}_querybar')">重置</a>
				</td>
#end##$velocityCount...
#if($velocityCount % $maxColumn == 0 || $velocityCount == $conditions.size())
			</tr>
#end##$velocityCount % $maxColumn...
#end##foreach
		</table>
	</div>

	<div style="margin-bottom:5px">
  		<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="add${Table}Item${rnd}()">添加</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="edit${Table}Item${rnd}()">编辑</a>
		<a href="#" class="easyui-linkbutton" iconCls="icon-copy" plain="true" onclick="copy${Table}Item${rnd}()">复制</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-cross" plain="true" onclick="deleteItem({jgridId:'#${table}_${rnd}_grid', table:${table}Table${rnd}, idField:'$autoIdField', callback:function(){}})">删除</a>
        <a href="#" class="easyui-menubutton" iconCls="icon-export" data-options="menu:'#${table}_${rnd}_export'">导出</a>
        <div id="${table}_${rnd}_export">
			<div data-options="iconCls:'icon-excel'" onclick="export${Table}Query${rnd}('Excel')">Excel</div>
			<div data-options="iconCls:'icon-csv'" onclick="export${Table}Query${rnd}('CSV')">CSV</div>
		</div>
        <a href="#" class="easyui-menubutton" iconCls="icon-import" data-options="menu:'#${table}_${rnd}_import'">导入</a>
        <div id="${table}_${rnd}_import">
			<div data-options="iconCls:'icon-excel'" onclick="selectFile('#${table}_${rnd}_file', 'Excel')">Excel</div>
			<div data-options="iconCls:'icon-csv'" onclick="selectFile('#${table}_${rnd}_file', 'CSV')">CSV</div>
		</div>
	</div>
</div>

<div id="${table}_${rnd}_dlg" class="easyui-dialog" style="width:40%;height:80%" data-options="shadow:false,resizable:true,closed:true" buttons="#${table}_${rnd}_dlg_buttons">
     <form id="${table}_${rnd}_form" method="post" novalidate>
     	<table>
#set($Integer=0)
#foreach($field in $fields)
	#set($css="")
	#set($style="")
	#set($hstyle="")
	#set($hint="")
	#if(!$field.editable || ${field.category} == "Timestamp")
		#set($style="display:none")
	#end
	#if(${field.category} == "Comment")
		#set($hstyle="vertical-align:center")
	#end
	#if(${field.category} == "Date")
		#set($css="easyui-datebox")
	#elseif(${field.category} == "DateTime" || ${field.category} == "Timestamp")
		#set($css="easyui-datetimebox")
	#elseif(${field.category} == "Number")
		#set($css="easyui-numberspinner")
	#elseif(${field.category} == "Decimal")
		#set($css="easyui-numberbox")
	#else
		#set($css="easyui-validatebox")
	#end
#if($field.autoId)
	#set($style="display:none")
#end

			<tr style="$style">
				<th style="$hstyle">
					${field.text}
				</th>
	#if($field.radios)
				<td>
#foreach($item in $field.radios)
			<label><input id="${table}_${rnd}_${field.name}_input${velocityCount}" name="${field.name}" type="radio" value="${item.value}">${item.text}</label>
#end
			</td>
	#else
#if($field.min)
	#set($hint="最小值${field.min}")
#end
#if($field.max)
	#if($hint != "")
		#set($hint="$hint,最大值${field.max}")
	#else
		#set($hint="最大值${field.max}")
	#end
#end
#if(${field.precision} && $Integer.parseInt(${field.precision}) > 0)
	#set($hint="${hint}(${field.precision}位小数)")
#end
#if($field.category == "Comment")
			<td><textarea id="${table}_${rnd}_${field.name}_input" style="width:95%" name="${field.name}" rows="3"></textarea></td>
#else
			<td><input id="${table}_${rnd}_${field.name}_input" style="width:95%" name="${field.name}" class="$css" data-options="${field.option}"></td>
#end
	#end
			<td><span class="hint">$hint</span></td>
		</tr>
#end
		</table>
     </form>
 </div>
 <div id="${table}_${rnd}_dlg_buttons">
     <a id="${table}_${rnd}_save" href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-save">保存</a>
     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-no" onclick="closeDialog('#${table}_${rnd}_dlg')">取消</a>
 </div>



